home *** CD-ROM | disk | FTP | other *** search
- #include <SCSI.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
-
- #include "SCSIDefines.h"
- #include "SCSIPrototypes.h"
-
- short gSCSIID;
-
- short FindTape(char *name, short *id)
- {
- unsigned char string[32];
- short i;
-
- name[0] = 0;
- for (gSCSIID = 0; gSCSIID < 7; gSCSIID++)
- {
- *id = gSCSIID;
- if (Inquiry(string, sizeof(string)) != OK) continue;
- if (string[0] == 1)
- {
- memcpy(name, &string[8], 8);
- name[8] = 0;
- for (i = 7; i >= 0; i--)
- {
- if (isalnum(name[i])) break;
- name[i] = 0;
- }
- return OK;
- }
- }
- return ERROR;
- }
-
- short Rewind(void)
- {
- while (UnitReady() == ERROR);
- if (Select(gSCSIID) != OK) return ERROR;
- Command(REWIND, 0, 0, 0, 0, 0);
- return Complete(TIMEOUT);
- }
-
- short WriteBlock(void *ptr)
- {
- while (UnitReady() == ERROR);
- if (Select(gSCSIID) != OK) return ERROR;
- Command(WRITE, 1, 0, 0, 1, 0);
- Write(BLOCKSIZE, 1, ptr);
- return Complete(TIMEOUT);
- }
-
- short WriteBlocks(void *ptr, unsigned short blocks)
- {
- unsigned char *lsb, *msb;
-
- while (UnitReady() == ERROR);
- msb = (unsigned char*)(&blocks);
- lsb = msb + 1;
- if (Select(gSCSIID) != OK) return ERROR;
- Command(WRITE, 1, 0, *msb, *lsb, 0);
- Write(BLOCKSIZE, (long)blocks, ptr);
- return Complete(TIMEOUT);
- }
-
- short WriteMark(unsigned short number)
- {
- unsigned char *lsb, *msb;
-
- while (UnitReady() == ERROR);
- msb = (unsigned char*)(&number);
- lsb = msb + 1;
- if (Select(gSCSIID) != OK) return ERROR;
- Command(FILE_MARK, 0, 0, *msb, *lsb, 0);
- return Complete(TIMEOUT);
- }
-
- short Space(short number, unsigned char type) /* type = 0 blocks; type = 1 filemarks */
- {
- unsigned char *lsb, *msb, *hsb;
- long lnum;
-
- while (UnitReady() == ERROR);
- lnum = number;
- lnum *= 256;
- hsb = (unsigned char*)(&lnum);
- msb = hsb + 1;
- lsb = msb + 1;
- if (Select(gSCSIID) != OK) return ERROR;
- Command(SPACE, type, *hsb, *msb, *lsb, 0);
- return Complete(TIMEOUT);
- }
-
- short ReadBlock(void *ptr)
- {
- while (UnitReady() == ERROR);
- if (Select(gSCSIID) != OK) return ERROR;
- Command(READ, 1, 0, 0, 1, 0);
- Read(BLOCKSIZE, 1, ptr);
- return Complete(TIMEOUT);
- }
-
- short ReadBlocks(void *ptr, unsigned short blocks)
- {
- unsigned char *lsb, *msb;
-
- while (UnitReady() == ERROR);
- msb = (unsigned char*)(&blocks);
- lsb = msb + 1;
- if (Select(gSCSIID) != OK) return ERROR;
- Command(READ, 1, 0, *msb, *lsb, 0);
- Read(BLOCKSIZE, (long)blocks, ptr);
- return Complete(TIMEOUT);
- }
-
- short Inquiry(void *ptr, unsigned char nBytes)
- {
- while (UnitReady() == ERROR);
- if (Select(gSCSIID) != OK) return ERROR;
- Command(INQUIRY, 0, 0, 0, nBytes, 0);
- if (nBytes != 0) Read((long)nBytes, 1, ptr);
- return Complete(TIMEOUT);
- }
-
- short UnitReady(void)
- {
- if (Select(gSCSIID) != OK) return ERROR;
- Command(UNIT_READY, 0, 0, 0, 0, 0);
- return Complete(TIMEOUT);
- }
-
- short Erase(unsigned char restFlag)
- {
- while (UnitReady() == ERROR);
- if (restFlag) restFlag = 1;
- if (Select(gSCSIID) != OK) return ERROR;
- Command(ERASE, restFlag, 0, 0, 0, 0);
- return Complete(TIMEOUT);
- }
-
- short Select(short lun)
- {
- OSErr status;
-
- /* get bus */
-
- status = SCSIGet();
- if (status != noErr) return ERROR;
- #ifdef DEBUG
- printf("SCSIGet returns %d\n", status);
- #endif
- /* select device */
-
- status = SCSISelect(lun);
- if (status != noErr) return ERROR;
- #ifdef DEBUG
- printf("SCSISelect returns %d\n", status);
- #endif
- return OK;
- }
-
- void Command(unsigned char opCode, unsigned char byte1,
- unsigned char byte2, unsigned char byte3, unsigned char byte4,
- unsigned char controlByte)
- {
- SCSI_Command cmd;
- OSErr status;
-
- cmd.opCode = opCode;
- cmd.byte1 = byte1;
- cmd.byte2 = byte2;
- cmd.byte3 = byte3;
- cmd.byte4 = byte4;
- cmd.controlByte = controlByte;
-
- status = SCSICmd((Ptr)&cmd, sizeof(cmd));
- #ifdef DEBUG
- printf("SCSICmd returns %d\n", status);
- #endif
- }
-
- void Read(long blockSize, long numBlocks, void *bufferPtr)
- {
- SCSIInstr tib[3];
- OSErr status;
-
- tib[0].scOpcode = scInc;
- tib[0].scParam1 = (long)bufferPtr;
- tib[0].scParam2 = blockSize;
- tib[1].scOpcode = scLoop;
- tib[1].scParam1 = -10;
- tib[1].scParam2 = numBlocks;
- tib[2].scOpcode = scStop;
- tib[2].scParam1 = 0;
- tib[2].scParam2 = 0;
-
- status = SCSIRead((Ptr)tib);
- #ifdef DEBUG
- printf("SCSIRead returns %d\n", status);
- #endif
- }
-
- void Write(long blockSize, long numBlocks, void *bufferPtr)
- {
- SCSIInstr tib[3];
- OSErr status;
-
- tib[0].scOpcode = scInc;
- tib[0].scParam1 = (long)bufferPtr;
- tib[0].scParam2 = blockSize;
- tib[1].scOpcode = scLoop;
- tib[1].scParam1 = -10;
- tib[1].scParam2 = numBlocks;
- tib[2].scOpcode = scStop;
- tib[2].scParam1 = 0;
- tib[2].scParam2 = 0;
-
- status = SCSIWrite((Ptr)tib);
- #ifdef DEBUG
- printf("SCSIWrite returns %d\n", status);
- #endif
- }
-
- short Complete(long ticks)
- {
- OSErr status;
- short stat,message;
- short sense;
-
- status = SCSIComplete(&stat, &message, ticks);
- #ifdef DEBUG
- printf("SCSIComplete returns %d stat is %d message is %d\n", status,
- stat, message);
- #endif
-
- if ((stat & 2) != 0)
- {
- sense = Sense();
- return ERROR;
- }
- return OK;
- }
-
- short Sense(void)
- {
- unsigned char buffer[SENSE_BYTES];
- short i;
-
- Select(gSCSIID);
- Command(REQUEST_SENSE, 0, 0, 0, SENSE_BYTES, 0);
- Read(SENSE_BYTES, 1, buffer);
- #ifdef DEBUG
- for (i=0; i<SENSE_BYTES; i+=2) printf("%3d %3d %3d\n",
- i, buffer[i], buffer[i+1]);
- #endif
- Complete(TIMEOUT);
- return (short)buffer[0];
- }
-